package cn.daniellee.plugin.ice.core;

import cn.daniellee.plugin.ice.IceBaoxue;
import cn.daniellee.plugin.ice.component.ActionSender;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.math.BigDecimal;

public class BaoxueCore {

    public static void useBaoxue(Player player, String suction) {
        if (!player.isDead()) {
            Location location = player.getLocation();
            Location target = calcLocation(location);
            player.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, target.getX(), target.getY() + 1.5, target.getZ(), IceBaoxue.getInstance().suctionValue.get(suction).intValue(), 0, 0, 0, 0.01);
            ActionSender.send(player, IceBaoxue.getInstance().getConfig().getString("message.use-notify", "&eA mouthful of &bICE Vape &eis rich and mellow, silky smooth, awesome!").replace("&", "§"));
        }
    }

    public static void useViolent(Player player) {
        if (!player.isDead()) {
            Location location = player.getLocation();
            Location target = calcLocation(location);
            player.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, target.getX(), target.getY() + 1.5, target.getZ(), IceBaoxue.getInstance().suctionValue.get("v").intValue(), 0, 0, 0, 0.01);
            ActionSender.send(player, IceBaoxue.getInstance().getConfig().getString("message.violent-notify", "&eTake a violent sip of &bICE Vape &eand enjoy the smoky happiness!").replace("&", "§"));
        }
    }

    private static Location calcLocation(Location location) {
        double yaw = location.getYaw();
        double pitch = location.getPitch();
        double distance = -0.5;
        double hypotenuse = new BigDecimal(Math.cos(Math.toRadians(pitch))).multiply(new BigDecimal(distance)).doubleValue();
        double angle = yaw > 0 ? new BigDecimal(360).subtract(new BigDecimal(yaw % 360)).doubleValue() : - yaw % 360;
        double y = new BigDecimal(location.getY()).add(new BigDecimal(Math.sin(Math.toRadians(pitch))).multiply(new BigDecimal(distance))).doubleValue();
        double x = new BigDecimal(location.getX()).subtract(new BigDecimal(Math.sin(Math.toRadians(angle))).multiply(new BigDecimal(hypotenuse))).doubleValue();
        double z = new BigDecimal(location.getZ()).subtract(new BigDecimal(Math.cos(Math.toRadians(angle))).multiply(new BigDecimal(hypotenuse))).doubleValue();
        return new Location(location.getWorld(), x, y, z);
    }
}
